import { pinyin } from './pinyin.js';
export default {
  chineseToPinYin: function(l1) {
    var l2 = l1.length;
    var I1 = '';
    var reg = new RegExp('[a-zA-Z0-9]');
    for (var i = 0; i < l2; i++) {
      var val = l1.substr(i, 1);
      var name = this.arraySearch(val, pinyin);
      if (reg.test(val)) {
        I1 += val;
      } else if (name !== false) {
        I1 += name;
      }
    }
    I1 = I1.replace(/ /g, '-');
    while (I1.indexOf('--') > 0) {
      I1 = I1.replace('--', '-');
    }
    return I1;
  },
  arraySearch: function(l1, l2) {
    for (var name in pinyin) {
      if (pinyin[name].indexOf(l1) !== -1) {
        return this.ucfirst(name);
      }
    }
    return false;
  },
  ucfirst: function(l1) {
    if (l1.length > 0) {
      var first = l1.substr(0, 1).toUpperCase();
      var spare = l1.substr(1, l1.length);
      return first + spare;
    }
  },
  // 首字母
  getPY: function(name) {
    let pinYin = this.chineseToPinYin(name);
    let SX = '';
    let c = pinYin.charAt(0);
    return c
  },
  // 返回我的好友数组列表
  getDataList: function(list) {
    let arr = [
      [],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],
    ]
    let count = 0
    list.forEach(item=>{
      let py = ''
      if(item.inspectionContents) { // 这个地方根据后台返回的姓名字段来填写
		var reg1 = new RegExp('[A-Z]');
		var reg2 = new RegExp('[a-z]');
		if(reg1.test(item.inspectionContents.substr(0,1))) py = item.inspectionContents.substr(0,1);
		else if(reg2.test(item.inspectionContents.substr(0,1))) py = item.inspectionContents.substr(0,1).toUpperCase(); 
        else py = this.getPY(item.inspectionContents);
      }else {
        py = "#"
      }
      switch(py){
        case 'A': 
          arr[0].push(item)
          break;
        case 'B':
          arr[1].push(item)
          break;
        case 'C':
          arr[2].push(item)
          break;
        case 'D':
          arr[3].push(item)
          break;
        case 'E':
          arr[4].push(item)
          break;
        case 'F':
          arr[5].push(item)
          break;
        case 'G':
          arr[6].push(item)
          break;
        case 'H':
          arr[7].push(item)
          break;
        case 'I':
          arr[8].push(item)
          break;
        case 'J':
          arr[9].push(item)
          break;
        case 'K':
          arr[10].push(item)
          break;
        case 'L':
          arr[11].push(item)
          break;
        case 'M':
          arr[12].push(item)
          break;
        case 'N':
          arr[13].push(item)
          break;
        case 'O':
          arr[14].push(item)
          break;
        case 'P':
          arr[15].push(item)
          break;
        case 'Q':
          arr[16].push(item)
          break;
        case 'R':
          arr[17].push(item)
          break;
        case 'S':
          arr[18].push(item)
          break;
        case 'T':
          arr[19].push(item)
          break;
        case 'U':
          arr[20].push(item)
          break;
        case 'V':
          arr[21].push(item)
          break;
        case 'W':
          arr[22].push(item)
          break;
        case 'X':
          arr[23].push(item)
          break;
        case 'Y':
          arr[24].push(item)
          break;
        case 'Z':
          arr[25].push(item)
          break;
        default:
          arr[26].push(item)
      }
 
    })
	
	let result = [];
	for(let i=0; i<26 ;i++){
		result.push({
			title: String.fromCharCode(65+i),
			data: arr[i]
		})
	}
	result.push({
		title: '#',
		data: arr[26]
	})
	
    return result;
  }
};